package org.zjh.controller.timer;

import org.zjh.controller.GameController;
import org.zjh.model.Player;
import org.zjh.model.Poker;
import org.zjh.model.RealPlayer;
import org.zjh.utils.PokerUtil;
import org.zjh.view.game.GamePanel;
import org.zjh.view.ui.GameUi;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;


/**
 * @author 郑佳豪
 * @version 1.0
 * @description: 分别给三个玩家发牌定时器
 * @date 2024/10/11 17:08
 */
public class DealPokersTimer {
    private Timer timer;
    private GamePanel gamePanel;
    ActionListener actionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            GameController gameController = GameController.getInstance();
            int totalDealtCards = gameController.getPlayer().getPokerList().size()
                    + gameController.getComputerA().getPokerList().size()
                    + gameController.getComputerB().getPokerList().size();

            // 如果三位玩家手上的牌等于51张 定时器停止
            if (totalDealtCards == 51) {
                timer.stop(); // 发完51张牌停止计时
                PokerUtil.sortPoker(gameController.getPlayer()); // 排序真人玩家的手牌
                PokerUtil.sortPoker(gameController.getComputerA()); // 排序电脑玩家的手牌
                PokerUtil.sortPoker(gameController.getComputerB()); // 排序电脑玩家的手牌


                //短暂定时器 等待延迟叫分标签显示 就是等待扑克牌排好序之后再显示 叫分标签
                new Timer(1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        //显示倍数标签
                        gamePanel.getMultipleJLabel().setVisible(true);
                        gameController.getCallPointTimer().start();
                        ((Timer) e.getSource()).stop();
                    }
                }).start();

                return;
            }

            //获取发牌时单次发的扑克牌
            Poker poker = gameController.getPokerBoxList().get(gameController.getCurrentPokerIndex());

            //判断这张牌是否是叫分牌 如果是把当前玩设置为 最先叫分的玩家
            if (poker.isCallPointPoker()) {
                //设置叫分玩家为 当前拿到叫分牌的玩家
                gameController.setCallPointCurrentPlayer(gameController.getCurrentPlayer());
            }

            // 根据玩家类型设置目标位置
            //判断类型 如果时真人玩家类型设置 扑克牌属性
            //获得当前玩家
            Player currentPlayer = gameController.getCurrentPlayer();
            if (currentPlayer instanceof RealPlayer) {
                //设置发牌到对应位置
                new MoveObjTimer(poker, 250 + gameController.getCurrentPlayer().getPokerList().size() * 35, 500, gamePanel);
                //设置扑克牌状态正面朝上
                poker.setUp(true);
                //发牌时候不可以被点击
                poker.setCanBeClick(false);
                //调用扑克牌反转方法
                poker.turnFront();
                //设置扑克牌 标识符
                poker.setOwnerId("REAL");
            } else if (currentPlayer == gameController.getComputerA()) {
                new MoveObjTimer(poker, 50, 25 + gameController.getCurrentPlayer().getPokerList().size() * 20, gamePanel);
                //设置扑克牌 标识符
                poker.setOwnerId("CPA");
            } else {
                new MoveObjTimer(poker, 1050, 25 + gameController.getCurrentPlayer().getPokerList().size() * 20, gamePanel);
                //设置扑克牌 标识符
                poker.setOwnerId("CPB");
            }
            //添加手牌到 当前玩家集合中
            currentPlayer.addPoker(poker);

            gamePanel.setComponentZOrder(poker, 0);
            gameController.setCurrentPlayer(GameController.getInstance().getCurrentPlayer().getNextPlayer());  // 切换到下一个玩家
            gameController.setCurrentPokerIndex(GameController.getInstance().getCurrentPokerIndex() + 1); // 发下一张牌

            if (gameController.getCurrentPokerIndex() >= 51) {
                //将剩下的三张牌移动到地主牌位置上
                ArrayList<Poker> dzPokerList = gameController.getLandOwnerPokerList();
                for (int i = 0; i < dzPokerList.size(); i++) {
                    Poker DZPoker = dzPokerList.get(i);
                    new MoveObjTimer(DZPoker, 400 + i * 150, 25, gamePanel);
                }
            }

        }

    };





    public DealPokersTimer() {
        timer = new Timer(150, actionListener);
    }

    public Timer getTimer() {
        this.gamePanel = GameUi.gameFrame.getGamePanel();
        return timer;
    }

    public void setGamePanel(GamePanel gamePanel) {
        this.gamePanel = gamePanel;
    }
}
